/**
 * service  js file;
 * company:junqitecch
 * author:xuzuotao
 */
/*
 * 导入各个功能模块
 */
var  request = require('./request');
var  response = require('./response'); 
var  consts  = require('../consts');
var  errorProcess = require('./service_error');
var  mainpageProcess = require('./service_main_page');
var  product = require('./service_product');
var  login= require('./service_login');
var  user= require('./service_user');
var  usersafety=require('./service_safety');
var  tournote=require('./service_tournote');
var  tournote_comment=require('./service_tournote_comment');
var  order=require('./service_order');
var  favorite=require('./service_favorite');
var  message = require('./service_message');
var  member=require('./service_member');
var  address=require('./service_address');
var  promotion=require('./service_promotion');
var  userMember=require('./service_user_member');

async function delay(count){
	var promise = new Promise(function(resolve, reject){

		setTimeout( function(){
		    console.log(1);
		    resolve('ok');
		},count);

	});

	return promise;
}

async function router(partner,data, callback){
//	如果有回调则处理
	if(callback){
//		通过调用request模块的pareseRequest方法将data转换为request结构的对象
		
		var obj = request.parserRequest(data);
		if(!obj.seq){
			obj.seq = new Date().getTime();
		}
		var link = partner.split('/'); //代理商 级别以/分开如果只有/则为信马由缰xmyj
		var single='jqepay';

		console.log('request::'+JSON.stringify(obj));
		if(link.length>0){
			single = link[link.length-1];
			if(single.length<=0)single='jqepay';
		}
		
//		调用handleService处理路由功能
		async function cb(res){
//			调用response的toResonse方法生成Response对象的json字符串
			var string = response.toResponse(res);
			
			if(string && string.length>2048){
				console.log('response11::  '+ res.seq +'::'+  string.substring(0,2048));

			}else{
				console.log('response:: '+ res.seq +'::'+ string);

			}
			console.log('page  '+consts.Service.LOGIN);

			//await delay(4000);

			callback(string);
		
		};
		
		handleService(single,obj,cb);
	
	}
}

//导出router函数 partner是合作商户通过url的中/后面的字符串得到
module.exports.router= router;
//路由部分的核心功能根据op操作数将不同的业务转换为不同的服务调用
async function handleService(partner,reqObj, callback){
	var obj = {};
	obj.ver=reqObj.ver;
	obj.errorCode=0;
	obj.seq=reqObj.seq;
	obj.ver= reqObj.ver;
	obj.op= reqObj.op;
	console.log('mmmaaaa '+reqObj.op);
	

	switch(reqObj.op){
	//调用错误处理模块
		case consts.Service.ERROR_OP:
			errorProcess.errorOp(reqObj,obj,callback);
			break;
	//获取首页信息
		case  consts.Service.MAINPAGE:
			mainpageProcess.processMainPage(reqObj,obj,callback);
			break;
			
			//获取首页信息
		case  consts.Service.MAINPAGE_BUYER:
			mainpageProcess.processMainPageBuyer(reqObj,obj,callback);
			break;			
	//获取商品（活动)列表		
		case consts.Service.PRODUCT_LIST:
			product.processProductList(reqObj,obj,callback);
			break;
			//获取菜品分类列表(menu)		
		case consts.Service.CATALOG_LIST:
			product.processCatalogList(reqObj,obj,callback);
			break;	
	//获取商品（活动）详情		
		case consts.Service.PRODUCT_DETAIL:
			product.processDetail(reqObj,obj,callback);

			break;
			
		case consts.Service.PRODUCT_UPDATE:
			product.processUpdate(reqObj,obj,callback);

			
			break;
		case consts.Service.PRODUCT_SALE:
			product.processSale(reqObj,obj,callback);

			
			break;		
	
		case consts.Service.CHECK_SMS_RETRIEVE:
		case consts.Service.CHECK_EMAIL_RETRIEVE:
			login.processCheckCode(reqObj,obj,callback);
		break;
		case consts.Service.LOGIN:
			login.processLogin(reqObj,obj,callback);
			break;
//		case consts.Service.REGISTER:
//			login.processRegister(reqObj,obj,callback);
//
//			break;
		case consts.Service.OAUTHCODE_LOGIN:

			login.processOAUTHCodeLogin(reqObj,obj,callback);
		break;
		case consts.Service.OAUTHWX_SHARE:
			login.processOAUTHShare(reqObj,obj,callback);
		
			break;
		case consts.Service.LOGOUT:
			login.processLogout(reqObj,obj,callback);
			break;
		case consts.Service.USER_RESET_PASSWORD:
			login.processResetPassword(reqObj,obj,callback);
			break;
		case consts.Service.USER_REFUND_UPDATE_PWD:
			user.processRefundUpdatePwd(reqObj,obj,callback);
			break;
		case consts.Service.USER_REFUND_VERIFY_PWD:	
			user.processRefundVerifyPwd(reqObj,obj,callback);

			break;
		case consts.Service.USER_DETAIL:
			user.processUserDetail(reqObj,obj,callback);
		
			break;
		case consts.Service.USER_CHANGE_PASSWORD:
			usersafety.processChangePassword(reqObj,obj,callback);
			break;		
		
		case consts.Service.MEMBER_DEALINGS:
			member.processDealings(reqObj,obj,callback);
			
			break;
		case consts.Service.MEMBER_COLLECT_COUPON:
			member.processCollectCoupon(reqObj,obj,callback);
			
			break;	
				
			//短信发送注册、登录、退出等功能
		//case consts.Service.SEND_SMS_RETRIEVE:processCouponList   //
			
		case consts.Service.MEMBER_LIST:
			member.processList(reqObj,obj,callback);
	
			break;	
		
			
		case consts.Service.MEMBER_UPDATE:
			member.processUpdate(reqObj,obj,callback);
	
			break;			
		case consts.Service.MEMBER_ADD:
			member.processAdd(reqObj,obj,callback);
	
			break;			
			
		case consts.Service.MEMBER_LIST_COUPONS:
			member.processCouponList(reqObj,obj,callback);
	
			break;	
			
		case consts.Service.MEMBER_LIST_MYCOUPONS:
			member.processMyCoupons(reqObj,obj,callback);
	
			break;		
			
	        	
		case consts.Service.MEMBER_BIND:
			member.processBind(reqObj,obj,callback);
        	break;
		case consts.Service.MEMBER_OFFICIAL_LOGIN:
			member.processOfficialLogin(reqObj,obj,callback);
        	break;
        	
		case consts.Service.MEMBER_DETAIL:
			member.processDetail(reqObj,obj,callback);

        	break;
        	
/*
 *        BUYEER_REGION_LIST:12,
        BUYER_ADDRESS_DETAIL:13,
        BUYER_ADDRESS_EDIT:14,
        BUYER_ADDRESS_LIST:15,
 */        	
	
 
		case consts.Service.BUYER_ADDRESS_LIST:
			address.processList(reqObj,obj,callback);

        	break; 	 	      	
		case consts.Service.BUYER_ADDRESS_DETAIL:
			address.processDetail(reqObj,obj,callback);

        	break; 	 	
        	
		case consts.Service.BUYER_ADDRESS_EDIT:
			address.processAddressEdit(reqObj,obj,callback);

        	break; 		
		case consts.Service.BUYEER_REGION_LIST:
			address.processRegionList(reqObj,obj,callback);

        	break; 	
	
		case consts.Service.MEMBER_REGISTER:
			member.processRegister(reqObj,obj,callback);

        	break; 	
        	
		case consts.Service.USER_PAYSIGN_LIST:
			usersafety.processPaySignList(reqObj,obj,callback);
			break;
		case consts.Service.USER_DETAIL_UPDATE:
			user.processUserDetailUpdate(reqObj,obj,callback);

        	break;
		case consts.Service.USER_DETAIL_LOOK:	
         	user.processUserLook(reqObj,obj,callback);
        	break;	
		case consts.Service.USERS_LOOK:
			user.processUsersLook(reqObj,obj,callback);
			break;
		case consts.Service.USER_CHANGE_MOBILE:
			usersafety.processChangeMobile(reqObj,obj,callback);

        	break;
//	        收藏处理
		case consts.Service.MEMBER_VIP_CHECK: ///USER_VIP_CHECK FAVORITE_PRODUCT_ADD:
			member.processVIPCheck(reqObj,obj,callback);

	        break;
	        
		case consts.Service.MEMBER_COLLECT_GIFT: ///USER_VIP_CHECK FAVORITE_PRODUCT_ADD:
			member.processCollectGift(reqObj,obj,callback);

	        break;    
	        
        	/*
      * 游迹相关的
      */   	
//        获取游迹列表
		case consts.Service.MESSAGE_LIST:
			message.processList(reqObj,obj,callback);

        	break;
//       发表游迹 	
		case consts.Service.TOURNOTE_PUBLISH:
			tournote.processPublish(reqObj,obj,callback);

        	break;
//        删除游迹	


		case consts.Service.MESSAGE_OFFICIAL:   
			message.processOfficial(reqObj,obj,callback);//tournote.processMine(reqObj,obj,callback);

        	break;
	    	
//        游迹评论
		case consts.Service.TOURNOTE_COMMENT:
			tournote_comment.processComment(reqObj,obj,callback);

			break;

		case consts.Service.TOURNOTE_COMMENT_DEL:
			tournote.processDetail(reqObj,obj,callback);

        	break;	
			
		case consts.Service.ORDER_EARNINGSSTATISTICS:   //TOURNOTE_COMMENT_LIST
			
			order.processOrderEarningStatistics(reqObj,obj,callback);
		
			break;
			
			
		case consts.Service.ORDER_EARNINGSSTATISTICSREPORT:   //TOURNOTE_COMMENT_LIST
			
			order.processOrderEarningStatisticsReport(reqObj,obj,callback);
		
			break;	
			
			
		case consts.Service.ORDER_PAYSTATISTICSREPORT:   //TOURNOTE_COMMENT_LIST
			
			order.processOrderPayStatisticsReport(reqObj,obj,callback);
		
			break;		
       
			
		case consts.Service.PRODUCT_SALESSTATISTICSREPORT:  
			
			product.processSalesStatisticsReport(reqObj,obj,callback);
		
			break;		
			
	    case consts.Service.PRODUCT_SALESRANKINGREPORT:  
			
			product.processSalesRankingReport(reqObj,obj,callback);
		
			break;	
			
				
//		case consts.Service.TOURNOTE_PRAISE:
//			tournote_comment.processPraise(reqObj,obj,callback);
//
//			break;
		

//订单相关的处理
//      下单  	
		case consts.Service.ORDER_PLACE:
			order.processOrderPlace(reqObj,obj,callback);
    		break;
    	
    		
		case consts.Service.ORDER_UPDATE:
			order.processOrderUpdate(reqObj,obj,callback);
    		break;
		case consts.Service.ORDER_STATUS_PRINT:
			order.processOrderUpdatePrintStatus(reqObj,obj,callback);
    		break;    		
    	
		case consts.Service.ORDER_UPDATE_DESKORPUSH:
			order.processOrderUpdateDeskOrPush(reqObj,obj,callback);
    		break;    		
    				
		case consts.Service.ORDER_MERGE:
			order.processOrderMerge(reqObj,obj,callback);
    		break;    		
    					
		case consts.Service.ORDER_APPOINT:
			order.processOrderAppoint(reqObj,obj,callback);
    		break;	
		case consts.Service.ORDER_DETAIL:
			order.processOrderDetail(reqObj,obj,callback);
    		break;
    		
		case consts.Service.ORDER_DETAILS:
			order.processOrderDetails(reqObj,obj,callback);
    		break;   		
    		
		case consts.Service.ORDER_CHANGE_PRINTER:
			order.processOrderDetailPrinter(reqObj,obj,callback);
    		break;	
    		
		case consts.Service.ORDER_PAY_LIST:
			order.processPayOrderList(reqObj,obj,callback);

			break;
			
		case consts.Service.ORDER_RESERVE_LIST:
			order.processReserveList(reqObj,obj,callback);

			break;	
		case consts.Service.ORDER_GIFT_LIST:
			order.processGiftList(reqObj,obj,callback);

			break;		
			
		case consts.Service.ORDER_STATUS_QUERY:
			order.processStatusQuery(reqObj,obj,callback);

			break;	
			
		case consts.Service.ORDER_CANCEL:
			order.processCancel(reqObj,obj,callback);

			break;					
		case consts.Service.ORDER_COUNTERPAY:
			order.processCounterPay(reqObj,obj,callback);

			break;					
			
						
			
		case consts.Service.ORDER_LIST_USER:
			order.processUserOrderList(reqObj,obj,callback);
			break;
			
		case consts.Service.REFRESH_LIST_USER1:
			user.processUserRefreshList1(reqObj,obj,callback);
			break;	
		case consts.Service.ORDER_STATISTICS:
			order.processUserOrderStatistics(reqObj,obj,callback);
			break;				
			
		case consts.Service.ORDER_STATISTICS1:
			order.processUserOrderStatistics1(reqObj,obj,callback);
			break;	
			
		case consts.Service.ORDER_PAY_STATISTICS:
			order.processUserPayStatistics(reqObj,obj,callback);

			break;
				
		case consts.Service.PAY_SUMMARY:
			order.processPaySummary(reqObj,obj,callback);

			break;
		case consts.Service.ORDER_REFUND_DECLARE:
			order.processRefundDeclare(reqObj,obj,callback);

			break;	

			
		case consts.Service.TRANSFERIN_LIST:
			order.processTransferinList(reqObj,obj,callback);

			break;	
			
		case consts.Service.TRANSFERIN_DETAIL:
			order.processTransferinDetail(reqObj,obj,callback);

			break;					
			
		case consts.Service.ORDER_LIST_BUYER:
	
			order.processBuyerOrderList(reqObj,obj,callback);

			break;
		
		case consts.Service.USER_STATISTICS_MEMBER_PAYMENTS:
			user.processMemberPaymentReport(reqObj,obj,callback);

        	break;
		

		case consts.Service.MEMBER_PAYMENTLIST: //TOURNOTE_DETAIL: 
			
			member.processPaymentList(reqObj,obj,callback);
        	break;
	
		case consts.Service.USER_MYSHOP_LOOK:
			user.processMyShopLook(reqObj,obj,callback);
		    	break;
		case consts.Service.USER_MYSHOP_UPDATE:
			user.processMyShopUpdate(reqObj,obj,callback);

	        break;
		case consts.Service.USER_MYSHOP_OPERATING:
			user.processMyShopOperating(reqObj,obj,callback);

	        break;
	        
		case consts.Service.USER_SET_PRINTCID:
			user.processSetPrinterCID(reqObj,obj,callback);

	        break;
		case consts.Service.USER_GET_CREDIT_LIST:
			user.processCreditList(reqObj,obj,callback);

	        break;    
	        
    
		case consts.Service.USER_BUYER_LIST:
			user.processUserBuyerList(reqObj,obj,callback);

	        break;
	        
		case consts.Service.USER_BUYER_DETAIL:
			user.processUserBuyerDetail(reqObj,obj,callback);  //processContactUpdate

	        break;

		case consts.Service.USER_COMPLETE_SERVICE:
			user.processCompleteService(reqObj,obj,callback);
			//favorite.processList();

	        break;
		case consts.Service.USER_COMPLETE_SERVICE1:
			user.processCompleteService1(reqObj,obj,callback);
			//favorite.processList();

	        break;     
	        
		case consts.Service.USER_START_SERVICE:
			user.processStartService(reqObj,obj,callback);
			//favorite.processList();

	        break;
     
		case consts.Service.USER_DESK_LIST:
			user.processDeskList(reqObj,obj,callback);
			     
	        break;
	        
		case consts.Service.USER_DESK_INFO:
			user.processDeskInfo(reqObj,obj,callback);
			     
	        break;    
	  
		case consts.Service.USER_DISCOUNT_LIST:
			user.processDiscountList(reqObj,obj,callback);
			     
	        break;     
	        
		case consts.Service.USER_BUILD_SCANURL:
			user.processBuildScanURL(reqObj,obj,callback);

	        break;
		case consts.Service.USER_PRINTER_LIST:
			user.processPrinterList(reqObj,obj,callback);
	        break;
	
		case consts.ORDER_PAY_FLOW:
			order.processUserOrderPayFlow();
			break;	        
	        
		case consts.Service.TEST://测试
			login.processTest(reqObj,obj,callback);
			break;
			
		case consts.Service.USER_MEMBER_LIST://list
			userMember.processList(reqObj,obj,callback);
			break;
		
		case consts.Service.USER_MEMBER_DETAIL://detail
			userMember.processDetail(reqObj,obj,callback);
			break;
			
		case consts.Service.USER_MEMBER_IMPORT://测试
			userMember.processImport(reqObj,obj,callback);
			break;
			
		case consts.Service.USER_MEMBER_UPDATE://测试
			userMember.processUpdate(reqObj,obj,callback);
			break;
			
		case consts.Service.USER_MEMBER_DELETE://测试
			userMember.processDelete(reqObj,obj,callback);
			break;		
		case 500://custome zhongguancun
			order.processUserOrderS500(reqObj,obj,callback);
			break;
    	
        case 501://custome zhongguancun
            order.processUserOrderS501(reqObj,obj,callback);
            
            break;
			
		default:
			errorProcess.errorOp(reqObj,obj,callback);
			//response.message="welcome "+partner;
//		    if(callback){
//		    	callback(response);
//		    }
			break;
		}
}
